<?php

require_once("include/@config.php");


function get_ws(&$con, $query, &$retval, &$sugest, &$bsug)
{
    $k=0;
    global $_WORDS;

    $words=explode(" ",$query);

    foreach($words as $w)
    {
        $sql="SELECT kw_id, wrd FROM kws WHERE wrd='{$w}';";
        //echo $sql;
        $result = mysqli_query($con,$sql) or die(mysqli_error($con));
        if(mysqli_num_rows($result)==0)
        {
            mysqli_free_result($result);
            $sql="SELECT kw_id, wrd FROM kws where soundex(wrd)=soundex('{$w}');";
            //echo $sql;
            $max_distance = 100;
            $near_word ="";
            $result = mysqli_query($con,$sql) or die(mysqli_error($con));
            while ($row2=mysqli_fetch_row($result))
            {
                $distance = levenshtein($row2[1], $w);
                if ($distance < 16)
                {
                    $retval[$row2[1]]=$row2[0];
                    $sugest.=$row2[1]." ";
                    $bsug=true;
                    break;
                }
            }
        }
        else
        {
            while ($row=mysqli_fetch_row($result))
            {

                $retval[$row[1]]=$row[0];
                $sugest.=$row[1]." ";
                break;
            }

        }
        if($k++ > $_WORDS)
            break;
    }
    //print_r($retval);

    mysqli_free_result($result);
}

function xx(&$c)
{
    echo("<pre><font color='red'>");
    if(is_array($c))
        print_r($c);
    else
        echo ($c);
    echo("</font><pre>");
}


 //tr_table($start,$r[0],$r[1],$r[2],$r[3],$r[4],$wght,$kwarr);
function tr_table($ord, $domain,$title,$linkname,$url,$txt,$wg,&$words)
{
    $ctn="";
    $wss="";
    foreach ($words as $w)
    {
        $i = strpos($txt, $w);
        $chk="";
        if($i != FALSE)
        {
            if(strlen($ctn))
                $chk = "..".substr($txt, $i, 128);
            else
                $chk = substr($txt, $i, 128);
        }

        $chk = preg_replace("/".$w."/i", "<font color='red'>".$w."</font>", $chk);
        $ctn.=$chk;
        $wss.=$w."+";
    }


    echo "<tr bgcolor='#FEA'>".
          "<td width='3%'>{$ord}</td>".
          "<td width='80%'><font size='-1'><a href='http://{$url}'>{$linkname}</a>( new-tab link: <a href='http://{$url}' ".
           "target='_blank'>http://{$url}</a>)</font></td>".
          "<td width='5%'>{$wg}</td></tr>";
    echo "<tr bgcolor='#FEC'><td colspan='3'><font size='-1'><a href='index.php?s={$domain}&w={$wss}' target='_blank'>Search:{$domain}</a></td></tr>";
    echo "<tr bgcolor='#FEE'><td colspan='3'>{$title}</td></tr>";
    echo "<tr><td colspan='3' bgcolor='#FEF'>";
    echo ("...{$ctn}");
    echo "</td></tr>";
}

function ex($w, $f, $n, $k, $s)
{
    $start = $f;
    $con = mysqli_connect(DB_HOST,DB_USER,DB_PASS);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    $result = mysqli_query($con,"USE ".DB_DATABASE.";") or die(mysqli_error($con));
    $warr=array();
    $sugest=null;
    $show=false;
    get_ws($con, $w, $warr, $sugest, $show);
   // xx($warr);

    if($show)
        echo "Mean: {$sugest}";
/*
l_id int not null,
kw_id int not null,
wg int(3),
s_id int(4),
lng int(1),
*/
    $x=0;
    $qry="";
    $ord="ORDER BY ";
    $kwarr=array();
    foreach($warr as $w=>$i)
    {
        $tbln="kwds".wtbl($w);
        $kwarr[$i]=$w;
        $qry.="(SELECT DISTINCT({$tbln}.s_id), {$tbln}.kw_id,{$tbln}.l_id,{$tbln}.wg FROM {$tbln} WHERE {$tbln}.kw_id={$i} ORDER BY {$tbln}.wg DESC)\n";
        $ord.= "wg{$x} DESC ";
        if($x++<count($warr)-1)
        {
            $qry.="UNION\n";
            $ord.=",";
        }
    }
    $qry.="ORDER BY wg DESC LIMIT {$f}, {$n};";

    //xx($qry);

    $lnks=array();
    $result = mysqli_query($con,$qry) or die(mysqli_error($con));
    $h=0;
    while ($r=mysqli_fetch_row($result))
    {
        $lnks[$h++] = "{$r[0]},{$r['1']},{$r[2]},{$r[3]},{$kwarr[$r[1]]}";

    }
    mysqli_free_result($result);
    //site_id, kw_id, link_id, weight,word
  //  xx($lnks);
/*
create table lnks (
l_id int auto_increment primary key not null,
s_id int,
z_id int(4),
p_id int(4),
url varchar(255) not null,
tit varchar(200),
dsc varchar(255),
name varchar(64),
txt mediumtext,
idat date,
size int(2),
md5 varchar(128),
vis int default 0,?w=staging home&f=8&n=8&s=0
dpth int default 0,
nlks int default 0,
*/
    $x=0;
    $maxw=0;
    $page=array();
    echo "<table width='80%'>";
    foreach($lnks as $w)
    {
        $s_id=strtok($w,",");
        $kw_id=strtok(",");
        $l_id=strtok(",");
        $wght=intval(strtok(","));
        $wrd=strtok(",");
        if($wght>$maxw)$maxw=$wght;

        $qry="SELECT DISTINCT(wss.sn),lnks.tit,lnks.name,lnks.url,lnks.txt FROM lnks,wss WHERE l_id={$l_id} AND wss.s_id={$s_id}";
        $result = mysqli_query($con,$qry) or die(mysqli_error($con));
        if($r=mysqli_fetch_row($result))
        {
            //xx($r);
            $start++;
            tr_table($start,$r[0],$r[1],$r[2],$r[3],$r[4],$wght,$kwarr);

           // tr_table(domain,title,linkname,url,txt,weight,words);
        }
        mysqli_free_result($result);
    }
    echo "</table>";
    mysqli_close($con);
    return $start;
}


?>

